package com.example.black.myapplication.myrevealview;

import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Gravity;

/**
 * <pre>
 *     author : 28476
 *     e-mail : hao.wang@1hai.cn
 *     time   : 2019/01/09/17:03
 *     desc   :
 *     version: 1.0
 * </pre>
 */

public class RevealDrawable extends Drawable {
    private Drawable unSelectedDrawable;
    private Drawable selectedDrawable;
    private int orientaion;
    public static final int HORIZONTAL = 1;
    public static final int VERTICAL = 2;

    public RevealDrawable(Drawable unSelectedDrawable, Drawable selectedDrawable, int orientation) {
        this.unSelectedDrawable = unSelectedDrawable;
        this.selectedDrawable = selectedDrawable;
        this.orientaion = orientation;
    }

    @Override
    public void draw(@NonNull Canvas canvas) {
        Rect bounds = getBounds();
        //左右为灰色部分
        if (getLevel() >= 10000 || getLevel() == 0){
            unSelectedDrawable.draw(canvas);
            return;
        }
        //中间为彩色部分
        if (getLevel() == 5000){
            selectedDrawable.draw(canvas);
            return;
        }
        int w = bounds.width();
        int h = bounds.height();
        //ratio -1 到0 到 1
        float ratio = (getLevel()/5000f) - 1f;

        w = (int) (w * Math.abs(ratio));
        int leftPart = ratio <0 ?Gravity.LEFT:Gravity.RIGHT;
        //灰色部分
        Rect leftRect = new Rect();
        Gravity.apply(
                leftPart
                ,w
                ,h
                ,bounds
                ,leftRect
        );
        canvas.save();
        canvas.clipRect(leftRect);
        unSelectedDrawable.draw(canvas);
        canvas.restore();

        int w1 = bounds.width();
        w1 = (int) (w1 * (1-Math.abs(ratio)));
        int rightPart =  ratio < 0 ?Gravity.RIGHT:Gravity.LEFT;
        Rect rightRect = new Rect();
        Gravity.apply(
                rightPart
               , w1
                ,bounds.height()
                ,bounds
                ,rightRect
        );
        canvas.save();
        canvas.clipRect(rightRect);
        selectedDrawable.draw(canvas);
        canvas.restore();

    }

    @Override
    protected void onBoundsChange(Rect bounds) {
        unSelectedDrawable.setBounds(bounds);
        selectedDrawable.setBounds(bounds);
    }

    @Override
    public void setAlpha(int alpha) {

    }

    @Override
    public int getIntrinsicHeight() {
        return Math.max(unSelectedDrawable.getIntrinsicHeight(),selectedDrawable.getIntrinsicHeight());
    }

    @Override
    public int getIntrinsicWidth() {
        return Math.max(unSelectedDrawable.getIntrinsicHeight(),selectedDrawable.getIntrinsicWidth());
    }

    @Override
    protected boolean onLevelChange(int level) {
        invalidateSelf();
        return true;
    }

    @Override
    public void setColorFilter(@Nullable ColorFilter colorFilter) {

    }

    @Override
    public int getOpacity() {
        return PixelFormat.UNKNOWN;
    }
}
